package csc.traning.wpsj.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.traning.wpsj.dao.Folder;
import csc.traning.wpsj.dao.User;
/**
 * Servlet implementation class ViewFileManagementServlet
 */
public class ViewFileManagementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String destination_dir_path ="";
    private File destinationDir;   
	private static double MAX_SIZE = 1073741824D;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewFileManagementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
                throw new ServletException("Required to login");
        }
        
        
        String folder = request.getParameter("folder");
        
        destination_dir_path = new StringBuffer("users/").append(user.getUserName()).append("/upload-folder").toString();
        destinationDir = new File(getServletContext().getRealPath(destination_dir_path));
		if(!destinationDir.isDirectory()) {
			destinationDir.mkdirs();
		}
		
		double capacity = 0;
		if(destinationDir.exists()){
			long folderSize = folderSize(destinationDir);
			capacity = folderSize/MAX_SIZE;
		}
		
		request.setAttribute("capacity", capacity);
		
        if(folder != null && !("").equals(folder)){
       	 
       	 String[] folers =  folder.split("/");
       	 List<Folder> navi = new ArrayList<Folder>();
       	 String folderPath = "";
       	 for (String string : folers) {
       		 folderPath = folderPath + string + "/";
       		 navi.add(new Folder(string, folderPath));
       	 }
       	 request.setAttribute("navi", navi);
       	 request.setAttribute("currentFolder", folder + "/");
       	 //String[] folers =  strfolder.split("_");
            //for (String folder : folers) {
           	destination_dir_path = new StringBuffer(destination_dir_path).append("/").append(folder).toString();
            //}
        }
        
        String realUploadPath = getServletContext().getRealPath(destination_dir_path);
        destinationDir = new File(realUploadPath);
       
        if(destinationDir.exists()){
       	 request.setAttribute("files", destinationDir.listFiles());
            request.getRequestDispatcher("pages/file_management.jsp").forward(request, response);
        }else{
       	 response.sendRedirect("file-management");
        }
		
		/*HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			throw new ServletException("Required to login");
		}
		
		DESTINATION_DIR_PATH = "users/" + user.getUserName() + "/upload-folder";
		String realUploadPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
		destinationDir = new File("C:/Download");
		double capicity = 0;
		if(destinationDir.exists()){
			long folderSize = folderSize(destinationDir);
			capicity = Math.round((folderSize/MAX_SIZE)*100);
		}
		
		request.setAttribute("capicity", capicity);
		request.setAttribute("files", destinationDir.listFiles());
		request.getRequestDispatcher("pages/file_management.jsp").forward(request, response);*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	public static long folderSize(File directory) {
	    long length = 0;
	    for (File file : directory.listFiles()) {
	        if (file.isFile())
	            length += file.length();
	        else
	            length += folderSize(file);
	    }
	    return length;
	}
}
